# coding=utf-8
"""
list.sort()
无返回值，对源数据进行排序
sorted()
有返回值，返回排序备份

sort(iterable,key=None,reverse=False)
Return a new list containing all items from the iterable in ascending order.

    A custom key function can be supplied to customize the sort order, and the
    reverse flag can be set to request the result in descending order.

"""

list1 = [1, 0, 9, 29, 3]
list2 = sorted(list1)
list3 = sorted(list1, reverse=True)
print(list2)
print(list3)
# 正数从小到大，负数从大到小
list1 = [1, 0, -9, 29, 3, -10, -2, -5]
list2 = sorted(list1, key=lambda x: (x < 0, abs(x)))
print(list2)


# 列表中自定义对象的排序
class Student():
    def __init__(self, name, age):
        self.name = name
        self.age = age


stu1 = Student('aaa', 19)
stu2 = Student('ccc', 20)
stu3 = Student('bbb', 18)

list1 = [stu1, stu2, stu3]
for stu in list1:
    print(stu.name, stu.age)
print("---------------------")
# list2 = sorted(list1,key=lambda x:x.age)
list2 = sorted(list1, key=lambda x: x.name)
for stu in list2:
    print(stu.name, stu.age)
